using GameDesigner;
using Net;
using Net.Common;
using Net.Entities;
using Net.Share;

namespace Example2
{
    public class ActorStateBase<T> : StateBehaviour where T : Actor
    {
        protected T self;

        public override void OnInit()
        {
            self = (T)Controller.Self;
        }

        public override void OnEnter()
        {
            self.AddOperation(new Operation(OperCommand.SwitchState, self.Identity) { index = state.Id, index1 = state.actionIndex });
        }
    }

    public class ActorActionBase<T> : ActionCoreBase
    {
        protected T self;

        public override void OnInit()
        {
            self = (T)Controller.Self;
        }
    }

    public class MonsterIdle : ActorStateBase<AIMonster>
    {
#if SERVER
        public override void OnUpdate()
        {
            if (self.target != null)
            {
                if (self.target.Property.IsDeath)
                {
                    self.target = null;
                    return;
                }
                var pos = self.target.transform.Position;
                self.transform.LookAt(new Vector3(pos.x, self.transform.Position.y, pos.z));
                var dis = Vector3.Distance(self.transform.Position, self.target.transform.Position);
                if (dis > 15f)
                {
                    self.target = null;
                }
                else if (dis < 1.5f)
                {
                    ChangeState(3);
                }
                else
                {
                    ChangeState(2);
                }
            }
            else
            {
                self.destination = new Vector3(RandomHelper.Range(self.pointCenter.x - 50f, self.pointCenter.x + 50f), 0f, RandomHelper.Range(self.pointCenter.z - 50f, self.pointCenter.z + 50f));
                if (self.Agent.SetDestination(self.destination))
                {
                    self.destination = self.Agent.Destination; //获取目的地位置, 这里才包含y的值
                    //if (roleInCount > 0)
                    //{
                    //    self.AddOperation(new Operation(OperCommand.EnterArea, self.Identity, self.transform.Position, self.transform.Rotation)
                    //    {
                    //        index = 2,
                    //        index1 = ActorID,
                    //        index2 = health,
                    //        direction = destination, //怪物下一个位置, 客户端收到后会自动寻路到目的地
                    //    });
                    //}
                }
            }
        }
#endif
    }

    public class MonsterWalk : ActorStateBase<AIMonster>
    {
#if SERVER
        public override void OnUpdate()
        {
            if (Time.time > self.idleTime)
            {
                ChangeState(0);
            }
        }
#endif
    }

    public class MonsterRun : ActorStateBase<AIMonster>
    {
#if SERVER
        public override void OnUpdate()
        {
            if (self.target != null)
            {
                var pos = self.target.transform.position;
                self.transform.LookAt(new Vector3(pos.x, self.transform.position.y, pos.z));
                var dis = Vector3.Distance(self.transform.position, self.target.transform.position);
                if (dis > 15f)
                {
                    ChangeState(0);
                }
                else if (dis < 1.5f)
                {
                    ChangeState(3);
                }
                else
                {
                    self.transform.Translate(0, 0, self.Property.MoveSpeed * Time.deltaTime);
                }
            }
        }
#endif
    }

    public class MonsterAttack : ActorActionBase<AIMonster>
    {
        public float distance = 3f;
        public float range = 90f;
        public float damage = 30f;
        //public GameObject damageEffect;

#if SERVER
        public override void OnAnimationEvent()
        {
            foreach (var gridObject in self.Grid)
            {
                var actor = gridObject as Actor;
                if (actor == self)
                    continue;
                var targetDir = actor.transform.position - self.transform.position; //必须是 攻击对象 - 自身对象
                if (targetDir.magnitude > distance)
                    continue;
                var forward = self.transform.Forward;
                var angle = Vector3.Angle(targetDir, forward); //最高是180
                if (angle < range & !actor.Property.IsDeath)//只能攻击本机玩家
                {
                    //var effect = Object.Instantiate(damageEffect, p.transform.position, p.transform.rotation);
                    //Object.Destroy(effect, 1f);
                    //if (self.targetID == ClientBase.Instance.UserId)
                    //    ClientBase.Instance.AddOperation(new Operation(Command.AIAttack, (int)damage));

                    actor.Damage(self, new ActorSystem.BuffBase<Actor>()
                    {
                        Self = self,
                        Target = actor
                    });
                }
            }
        }
#endif
    }

    public class MonsterDie : ActorStateBase<AIMonster>
    {
        public override void OnEnter()
        {
            base.OnEnter();
#if SERVER
            self.target = null;
#else
            self.View.GetComponent<UnityEngine.Collider>().enabled = false;
            self.View.GetComponent<UnityEngine.Rigidbody>().isKinematic = true;
#endif
        }

        public override void OnExit()
        {
#if SERVER
            self.target = null;
#else
            self.View.GetComponent<UnityEngine.Collider>().enabled = true;
            self.View.GetComponent<UnityEngine.Rigidbody>().isKinematic = false;
#endif
        }
    }
}